/*
 * Copyright (C) 拾光分享网 
 * All rights reserved, Designed By www.hiai.top.
 * Email：goodsking@163.com.
 */
package top.hiai.topic;

import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Page;

import top.hiai.common.model.Comments;
import top.hiai.config.BaseController;
import top.hiai.index.IndexService;
import top.hiai.my.favorite.MyFavoriteService;

/**
* @Author www.hiai.top
* @Email  goodsking@163.com
* @Message 更多资源尽在www.hiai.top
* @Introduction 类的介绍：话题分类控制器
*/

public class TopIcController extends BaseController{
	
	private TopIcService topIcService = new TopIcService();
	
	/**
	 * 显示所有的话题列表
	 */
	public void index(){
		Page<Comments> commentsPage = topIcService.commentsPage(getParaToInt("p", 1), limtCount);
		setAttr("commentsList", commentsPage);
		//获取热门的话题分类
		setAttr("commentsHot", IndexService.me.getCommentsHotList());
    	//热门文章
    	setAttr("articleHot", IndexService.me.getArticleHotList());
		render("index.html");
	}
	
	/**
	 * 显示分类下面的文章详情页面
	 */
	public void detail(){
		//获取ID
		Integer tid = getParaToInt();
		Comments comments  = topIcService.findById(tid);
		if(comments != null){
			//传标题
			setAttr("seoTitle", comments.get("name") + "下面的文章");
			setAttr("comments", comments);
			//获取该分类下面的文章
			setAttr("commentsArticleList",topIcService.getDetailCommentsArticle(getParaToInt("p", 1), limtCount, tid));
			//获取该分类下面的关注用户
			setAttr("commentsUserList",topIcService.getByConnentsFavoriteUser(tid));
			render("detail.html");
			setLikeAndFavoriteStatus(comments);
		}else{
			renderError(404);
		}
	}
	
    /**
     * 如果用户已登录，则需要显示当前 article 是否已经被该用户点赞、收藏了
     */
    private void setLikeAndFavoriteStatus(Comments comments) {
        Ret ret = Ret.create();
        MyFavoriteService.me.setFavoriteStatus(getLoginAccount(), "comments", comments, ret);
        setAttr("ret", ret);
    }
}